<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>10-raycaster射线</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    html,
    body {
      width: 100%;
      height: 100%;
    }
  </style>
</head>

<body>
  <script type="module">
    import * as THREE from "three";
    import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
    // 创建场景
    const scene = new THREE.Scene();
    // 创建相机
    const camera = new THREE.PerspectiveCamera(
      45,
      window.innerWidth / window.innerHeight,
      1,
      1000
    );
    camera.position.set(0, 20, 5);
    camera.lookAt(0, 0, 0);
    // 创建渲染器 
    const renderer = new THREE.WebGLRenderer({
      antialias: true
    });
    // 设置渲染器大小
    renderer.setSize(window.innerWidth, window.innerHeight);
    // 把渲染的canvas内容添加到body
    document.body.appendChild(renderer.domElement);

    // 立方体
    const sphere1 = new THREE.Mesh(
      new THREE.SphereGeometry(1, 32, 32),
      new THREE.MeshBasicMaterial({
        color: 0xffff33
      })
    );
    sphere1.position.x = -4;

    const sphere2 = new THREE.Mesh(
      new THREE.SphereGeometry(1, 32, 32),
      new THREE.MeshBasicMaterial({
        color: 0xffff33
      })
    );
    sphere2.position.set(0, 4, 0);

    const sphere3 = new THREE.Mesh(
      new THREE.SphereGeometry(1, 32, 32),
      new THREE.MeshBasicMaterial({
        color: 0xffff33
      })
    );
    sphere3.position.x = 4;

    scene.add(sphere1, sphere2, sphere3);

    const raycaster = new THREE.Raycaster();
    const rayOrigin = new THREE.Vector3(-6, 0, 0); // 原点向量
    const rayDirection = new THREE.Vector3(1, 0, 0); // 方向向量
    rayDirection.normalize(); // 归一化

    raycaster.set(rayOrigin, rayDirection);
    let meshs = [sphere1, sphere2, sphere3];
    

    // 创建轨道控制器
    const controls = new OrbitControls(camera, renderer.domElement);

    // 开启物体的阻尼动画效果 
    controls.enableDamping = true;

    // 监听controls的change事件
    controls.addEventListener("change", () => {
      // 交互的鼠标每次发生变化，都需要渲染一次(如果用了animate函数就不需要).
      renderer.render(scene, camera);
    });
    renderer.render(scene, camera);

    let moveY = 0;
    const clock = new THREE.Clock();

    function animate() {
      const intersects = raycaster.intersectObjects(meshs);

      for (const mesh of meshs) {
        mesh.material.color.set(new THREE.Color(0xffff33));
      }

      const elapseTime = clock.getElapsedTime();
      sphere1.position.y = 4 * Math.sin(elapseTime * 0.7);
      sphere2.position.y = 4 * Math.sin(elapseTime * 1.2);
      sphere3.position.y = 4 * Math.sin(elapseTime * 1.7);
      
      for(const intersectObject of intersects) {
        intersectObject.object.material.color.set(new THREE.Color(0xff0033));
      }

      requestAnimationFrame(animate);
      controls.update();
      renderer.render(scene, camera);
    } 

    // 添加动画并渲染
    animate();
  </script>
</body>

</html>